﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EITSurferInfinityLib
{
    public class st_datainterpreter_1:IDataInterpreter
    {
        int[,] IQA;
        int[,] IQB;
        short[,] raw;

        public EvaluationInfo Evalute(byte[] frame)
        {
            int offset=0; //is automatically incremented in ReadInteger32
            EvaluationInfo eval= EvaluationInfo.Unknown;
            int code=ReadInteger32(ref frame, ref offset);
            int TotalPacketSize=ReadInteger32(ref frame, ref offset);
            int TimeStamp=ReadInteger32(ref frame, ref offset);
            int Status=ReadInteger32(ref frame, ref offset);
            int ECGSize=ReadInteger32(ref frame, ref offset);
            int StretchSize=ReadInteger32(ref frame, ref offset);
            int BendSize=ReadInteger32(ref frame, ref offset);
            int PositionSize=ReadInteger32(ref frame, ref offset);
            int ScanningPatternCode=ReadInteger32(ref frame, ref offset);
            int VoltageInjectionSiteSize=ReadInteger32(ref frame, ref offset);
            int IQsize=ReadInteger32(ref frame, ref offset);
            int NumberExternalSignal=ReadInteger32(ref frame, ref offset);

            int IQVoltageInjectionOffset= offset + ECGSize +StretchSize+BendSize+PositionSize;
            int IQOffset= IQVoltageInjectionOffset+VoltageInjectionSiteSize;

            switch(code)
            {
                case 0:
                    IQA=new int[2,IQsize/8];
                    IQB = new int[2, VoltageInjectionSiteSize/8];
                    ReadInteger32Array(ref frame, IQOffset, IQsize, ref IQA);
                    ReadInteger32Array(ref frame, IQVoltageInjectionOffset, VoltageInjectionSiteSize, ref IQB);
                    eval= EvaluationInfo.IQA_IQB;
                    break;
                case 1:
                    raw= new short[2,IQsize/4];
                    ReadInteger16Array(ref frame, IQOffset, IQsize, ref raw);
                    eval= EvaluationInfo.Raw;
                    break;

            }
            return eval;
        }
        public int[,] GetIQA()
        {
            return IQA;
        }

        public int[,] GetIQB()
        {
            return IQB;
        }
        public short[,] GetRawData()
        {
            return raw;
        }

        int ReadInteger32(ref byte[] frame, ref int Offset)
        {
            int output=ReadInteger32(ref frame, Offset);
            Offset+=4;
            return output;
        }

        int ReadInteger32(ref byte[] frame, int Offset)
        {
            byte[] buffer = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                buffer[i] = frame[i + Offset];
            }
            return BitConverter.ToInt32(buffer, 0);
        }

        short ReadInteger16(ref byte[] frame, int Offset)
        {
            byte[] buffer = new byte[2];
            for (int i = 0; i < 2; i++)
            {
                buffer[i] = frame[i + Offset];
            }
            return BitConverter.ToInt16(buffer, 0);
        }

        void ReadInteger32Array(ref byte[] frame, int Offset, int count, ref int[,] output)
        {
            byte [] buffer =new byte[4];
            int p=0;
            for (int i = Offset; i < Offset+count; i=i+8)
            {
                output[0, p] = ReadInteger32(ref frame, i);
                output[1, p] = ReadInteger32(ref frame, i + 4);
                p++;
            }
            Offset+=4;
            //return BitConverter.ToInt32(buffer, 0);
        }
        void ReadInteger16Array(ref byte[] frame, int Offset, int count, ref short[,] output)
        {
            byte[] buffer = new byte[2];
            int p = 0;
            for (int i = Offset; i < Offset + count; i = i + 4)
            {
                output[0, p] = ReadInteger16(ref frame, i);
                output[1, p] = ReadInteger16(ref frame, i + 2);
                p++;
                //if (i == Offset + count - 4)
                //{
                //    string s = "";
                //}
            }
            Offset += 2;
            //return BitConverter.ToInt32(buffer, 0);
        }
}
}
